import json
import math
import time
from datetime import datetime
from log_time_sleeps.execl import Excel_xrlts


class Serce_Excel:
    def __init__(self, kwargs):
        self.__dict__.update(kwargs)

    def response_excel(self):
        # self.url_switch_one_data['dealStatus'] = '2'
        response = self.session.post(self.url_switch+'/wlwaq-server//logQuery/initByPage.action', headers=self.headers,
                                     data=self.url_switch_one_data, verify=False).json()['recordsTotal']
        if response:
            start_point = math.ceil(response // 50) * 50
            self.my_logg.info("安全工单正在生成")
            start_time = time.time()
            workbook_name = '安全告警'
            self.serce_xrlts=Excel_xrlts(svn_path=self.svn_path)
            data_list,data_lists,sheet=self.serce_xrlts.xlsx_read(workbook_name=workbook_name,searce_execl=True)
            index=self.serce_xrlts_miaoshu(data_lists)
            if index:
                self.serce_xrlts.xlsx_delete(sheet, index)
            for start in range(start_point, -1, -50):
                self.url_switch_one_data['start'] = str(start)
                response = self.session.post(self.url_switch+'/wlwaq-server//logQuery/initByPage.action', headers=self.headers,
                                             data=self.url_switch_one_data, verify=False).json()['data']
                if response:
                    response_dict = []
                    # 按照倒序遍历响应
                    for response_c in response[::-1]:
                        attack_type0 = response_c['brief']
                        insert_time1 = datetime.strptime(response_c['insert_time'], "%Y-%m-%d %H:%M:%S")
                        comment_list2 = None  # '已签收' 的时间
                        comment_list4 = None  # '已处置' 的时间
                        comment_list23 = json.loads(response_c['comment_list'])
                        # 遍历每条评论以处理动作
                        for comment_list234 in comment_list23:
                            action = comment_list234['action']
                            comment_date = comment_list234['date']
                            if action == '已处置':
                                comment_list4 = datetime.strptime(str(comment_date), "%Y-%m-%d %H:%M:%S")
                            elif action == '已签收':
                                comment_list2 = datetime.strptime(comment_date, "%Y-%m-%d %H:%M:%S")
                        response_dict.append([attack_type0, insert_time1, comment_list2, None, comment_list4])

            self.serce_xrlts.xlsx_copy(xlsx_dict=response_dict,data_list=data_list, sheet=sheet,searce_exec=True)
            end_time = time.time()
            self.my_logg.info("安全工单已生成完毕: {:.2f} 秒".format(end_time - start_time))
        else:
            self.my_logg.info("没有可以生成安全工单")



    def  serce_xrlts_miaoshu(self,data_lists):
        self.start_times = datetime.strptime(self.startTime, '%Y-%m-%d %H:%M:%S')
        for index, comparison_time in enumerate(data_lists[1:], start=1):
            if type(comparison_time) != datetime:
                if type(comparison_time) == str:
                    comparison_time_str_converted = comparison_time.replace('年', '-').replace('月', '-').replace('日', ' ').replace('时', ':').replace('分', ':').replace('秒', '')
                    # 现在我们可以将转换后的字符串解析为datetime对象
                    comparison_time = datetime.strptime(comparison_time_str_converted, '%Y-%m-%d %H:%M:%S')
                else:
                    print(type(comparison_time),comparison_time)
            if comparison_time > self.start_times:
                return index+1